package gsw.demo.mvp.core.net.rxservice;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.HashMap;

import gsw.demo.mvp.R;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ActivityRetrofitRxService extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retroft_rx);
    }

    /**
     * get请求
     * 需要在Applicaiton中初始化Config:
     * ConfigInit.init(this).withApiHost("http://dengpaoedu.com:8080/").configure();
     */
    public void click1(View view) {
        HashMap params = new HashMap();
        params.put("cityname", "长沙");
        params.put("key", "fd0f609b22905a0a56a48d7cf59a558b");
        RxRestClient.create()
                .url("/weather/")
                .params(params)
                .build()
                .get()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        //响应结果
                        Toast.makeText(ActivityRetrofitRxService.this, s, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(ActivityRetrofitRxService.this, "请求错误：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
